<template>
  <CollapseContainer :title="t('密码修改')" :canExpan="false">
    <div class="flex flex-col items-center justify-center py-8 bg-white">
      <BasicForm @register="register" />
      <div class="flex justify-center">
        <a-button @click="resetFields"> {{ t('重置') }} </a-button>
        <a-button class="!ml-4" type="primary" @click="handleSubmit"> {{ t('确认') }} </a-button>
      </div>
    </div>
  </CollapseContainer>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { BasicForm, useForm, FormSchema } from '/@/components/Form';
  import { CollapseContainer } from '/@/components/Container/index';
  import { PageEnum } from '/@/enums/pageEnum';
  import { useGo } from '/@/hooks/web/usePage';
  import { updatePassword } from '/@/api/system/user';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  export const formSchema: FormSchema[] = [
    {
      field: 'oldPassword',
      label: t('当前密码'),
      component: 'InputPassword',
      required: true,
      colProps: {
        span: 24,
      },
    },
    {
      field: 'newPassword',
      label: t('新密码'),
      component: 'StrengthMeter',
      componentProps: {
        placeholder: t('新密码'),
      },
      rules: [
        {
          required: true,
          message: t('请输入新密码'),
        },
      ],
      colProps: {
        span: 24,
      },
    },
    {
      field: 'confirmPassword',
      label: t('确认密码'),
      component: 'InputPassword',
      colProps: {
        span: 24,
      },

      dynamicRules: ({ values }) => {
        return [
          {
            required: true,
            validator: (_, value) => {
              if (!value) {
                return Promise.reject(t('密码不能为空'));
              }
              if (value !== values.newPassword) {
                return Promise.reject(t('两次输入的密码不一致!'));
              }
              // 验证 密码必须包含大写字母、小写字母、数字和特殊字符，长度8~16位
              if (value.length < 8 || value.length > 16) {
                return Promise.reject(t('密码须长度8~16位'));
              }
              let pwdCount = 0;
              let pwdRegex1 = new RegExp('(?=.*[0-9]).{8,16}'); //纯数字
              let pwdRegex2 = new RegExp('(?=.*[a-z]).{8,16}'); //小写字母
              let pwdRegex3 = new RegExp('(?=.*[A-Z]).{8,16}'); //大写字母
              let pwdRegex4 = new RegExp('(?=.*[^a-zA-Z0-9]).{8,16}'); //特殊字符
              if (pwdRegex1.test(value)) {
                pwdCount = pwdCount + 1;
              }
              if (pwdRegex2.test(value)) {
                pwdCount = pwdCount + 1;
              }
              if (pwdRegex3.test(value)) {
                pwdCount = pwdCount + 1;
              }
              if (pwdRegex4.test(value)) {
                pwdCount = pwdCount + 1;
              }
              if (pwdCount < 4) {
                return Promise.reject(t('密码须含大写,小写,数字和特殊字符'));
              }
              return Promise.resolve();
            },
          },
        ];
      },
    },
  ];

  export default defineComponent({
    name: 'ChangePassword',
    components: { BasicForm, CollapseContainer },
    setup() {
      const go = useGo();
      const [register, { validate, resetFields }] = useForm({
        size: 'large',
        labelWidth: 100,
        showActionButtonGroup: false,
        schemas: formSchema,
      });

      async function handleSubmit() {
        try {
          const values = await validate();
          // const { passwordOld, passwordNew } = values;

          // TODO custom api
          await updatePassword(values);

          go(PageEnum.BASE_pageError);
        } catch (error) {}
      }

      return { register, resetFields, handleSubmit, t };
    },
  });
</script>
